package com.xiaoyun.seleniumlinux.config.mq;

import cn.hutool.core.util.StrUtil;
import com.rabbitmq.client.Channel;
import com.xiaoyun.seleniumlinux.config.listener.TimeConsumingListener;
import com.xiaoyun.seleniumlinux.config.driver.WebDriverConfig;
import com.xiaoyun.seleniumlinux.enums.MqConstants;
import com.xiaoyun.seleniumlinux.pojo.MessageDTO;
import com.xiaoyun.seleniumlinux.service.WebDiverServer;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.events.EventFiringDecorator;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDate;

import static com.xiaoyun.seleniumlinux.enums.MqConstants.RETRY_MAX_COUNT;


/**
 * @author Xiaoyun461
 * @data 2022/7/11
 */
@Slf4j
@Service
public class SeleniumReceiveListener {


    @Autowired
    private WebDiverServer webDiverServer;



    @RabbitListener(
            queues = MqConstants.QUEUE_SELENIUM, ackMode = "MANUAL")
    public void receiveMsg(MessageDTO msg, Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        log.info("队列接收到消息：" + msg);
        if (StrUtil.equals(msg.getUnitId(), "1702")) {
            channel.basicAck(deliveryTag, false);
            return;
        }
//        WebDriver sourceWebDriver = webDriverConfig.createWebDriver();
//        EventFiringDecorator<WebDriver> eventFiringDecorator = new EventFiringDecorator<>(timeConsumingListener);
//        WebDriver targetWebDriver = eventFiringDecorator.decorate(sourceWebDriver);
        LocalDate nowDate = LocalDate.now();
        try {
//            webDiverServer.seleniumHtml(msg.getUrl(), msg.getUnitId(), sourceWebDriver, nowDate);
            webDiverServer.seleniumHtml(msg.getUrl(), msg.getUnitId(), nowDate);
//            rabbitMqMessageService.sendMessage(msg);
        } catch (Exception e) {
            //重试10次,自动失败
            if (deliveryTag > RETRY_MAX_COUNT) {
                try {
                    channel.basicNack(deliveryTag, false, false);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            } else {
                try {
                    //告诉mq本条消息消费失败
                    channel.basicNack(deliveryTag, false, true);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
            e.printStackTrace();
        } finally {
            channel.basicAck(deliveryTag, true);
//            webDriverConfig.quit(sourceWebDriver);
//            webDriverConfig.quit(targetWebDriver);
        }
        log.info("deliveryTag:{}", deliveryTag);
        if (deliveryTag > RETRY_MAX_COUNT) {
            try {
                channel.basicNack(deliveryTag, false, false);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }

        }
    }

}
